import { Component, EventEmitter, Input, OnInit, Output, TemplateRef, ViewChild } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { SFComponent, SFSchema } from '@delon/form';
import { MenuService } from '../../menu-manage/menu.service';
import { UtilService } from '@core/services/util.service';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import {AreaService} from "../../area-manage/area.service";

@Component({
  selector: 'app-door-edit',
  templateUrl: './door-edit.component.html',
  styles: []
})
export class DoorEditComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf: SFComponent;
  @Input() type : 0 | 1;
  @Input() set _param(value){
    if(!value) return;
    this.param=value;
  }
  param:object;
  schema: SFSchema = {
    properties: {
      doorDescribe: {
        type: 'string',
        title: '名称',
      },
      communityId: {
        type: 'string',
        title: '小区',
        enum:[],
        ui:{
          widget:'select',
          allowClear:true,
          change: (value) => {
            this.getbuilding(value,"houseId");
            const statusProperty = this.sf.getProperty('/areaId');
            statusProperty.schema.enum=[{label:'',value:''}];
            statusProperty.widget.reset("");
          }
        }
      },
      doorType: {
        type: 'string',
        title: '门禁类型',
        enum:[
          {label:'小区门禁',value:'2'},
          {label:'单元门禁',value:'1'},
        ],
      },
      houseId: {
        type: 'string',
        title: '楼宇',
        enum:[],
        ui:{
          widget:'select',
          allowClear:true,
          change: (value) => {
            this.getbuilding(value,"areaId");
          }
        }
      },
      areaId:{
        type:'string',
        title:'单元',
        enum:[],
        ui:{
          widget:'select',
          allowClear:true
        }
      }
    },
    required: ['doorDescribe', 'communityId', 'doorType'],
  };
  constructor(
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private menu: MenuService,
    private util: UtilService,
    private api: ApiService,
    private http: HttpClient,
    private area:AreaService
    ) { }

  ngOnInit() {
    this.getCommunity();
  }

    // 确定编辑功能
    formSubmit(data) {
      const url = this.type === 0 ? this.api.area.addDoor.url : this.api.role.update.url;
      const method = this.type === 0 ? this.api.area.addDoor.method : this.api.role.update.method;
      console.log(data);
      let communityName="";
      let houseName="";
      let areaName="";
      if(data.communityId != undefined){
        let arr:any = this.sf.getProperty('/communityId').schema.enum.filter((item:any)=>{
          return item.value == data.communityId;
        });
        communityName = arr[0].label;
      }
      if(data.houseId != undefined){
        let arr:any = this.sf.getProperty('/houseId').schema.enum.filter((item:any)=>{
          return item.value == data.houseId;
        });
        houseName = arr[0].label;
      }
      if(data.areaId != undefined){
        let arr:any = this.sf.getProperty('/areaId').schema.enum.filter((item:any)=>{
          return item.value == data.areaId;
        });
        areaName = arr[0].label;
      }
      const body={...data,communityName:communityName,houseName:houseName,areaName:areaName};
      console.log(body);
      this.http.request<any>(method, url,  {body}).subscribe((res:any) => {
        console.log(res)
        if (res.status === '200' || res.status === '201') {
          this.msg.success('操作成功');
          this.modalRef.triggerOk();
        } else {
          this.msg.error(res.message)
        }
      })
    }

    //获取小区列表,筛选组装数据
    getCommunity():void{
      this.http.post(this.api.area.queryList.url,{areaRank:6}).subscribe((res:any) => {
        console.log(res)
        if(res.status  == 200){
        const statusProperty = this.sf.getProperty('/communityId')!;
        statusProperty.schema.enum = res.data.list.map(item => {
          return {label:item.areaName,value:item.id};
        })
        statusProperty.widget.reset('');
      }
      })
    }

    //获取楼栋单元，组装数据
    getbuilding(id,propertyName){
      this.loadNode(id).then(data => {
        const statusProperty = this.sf.getProperty('/'+propertyName)!;
        statusProperty.schema.enum = data.map(item => {
          return {label:item.title,value:item.key};
        })
        statusProperty.schema.enum.push({label:'',value:''});
        statusProperty.widget.reset("");
      })
    }

    //获取楼栋单元
    loadNode(key): Promise <any>{
      return this.area.getAreaTreeNode(key)
    }

}
